package cn.dancepeak.course.service.impl;

import cn.dancepeak.anno.Log;
import cn.dancepeak.course.domain.Course;
import cn.dancepeak.course.mapper.CourseMapper;
import cn.dancepeak.course.query.CourseQuery;
import cn.dancepeak.course.service.ICourseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImplImpl implements ICourseServiceImpl {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    @Log
    public void add(Course course) {
        //新增
        courseMapper.insert(course);
    }

    @Override
    @Log
    public void update(Course course) {
        //用户名不能修改   禁用  在前端写
        courseMapper.updateById(course);
    }

    @Override
    @Log
    public void delete(Long id) {
        courseMapper.deleteById(id);
    }

    @Override
    @Log
    public void deleteByList(List<Long> ids) {
        courseMapper.deleteByList(ids);
    }

    @Override
    public Course getById(Long id) {
        return courseMapper.selectById(id);
    }

    @Override
    public PageInfo<Course> page(CourseQuery courseQuery) {
        // 1.下面这句代码表示开启分页，会将紧挨着这句代码的下一次数据库执行进行分页，切记一定要放在要分页的SQL语句前面执行
        // 2.执行SQL语句带条件查询，SQL语句不需要进行分页
        PageHelper.startPage(courseQuery.getCurrentPage(), courseQuery.getPageSize());
        List<Course> course = courseMapper.list(courseQuery);
        // 3.将查询结果转换为PageInfo对象，PageInfo对象中有前端分页所需要的各种参数
        PageInfo<Course> pageInfo = new PageInfo<>(course);
        // 4.响应总条数和分页信息
        return pageInfo;
    }

    @Override
    public List<Course> getCourse() {
        return courseMapper.getCourse();
    }
}
